package com.alibaba.taolele.controller;

import com.alibaba.taolele.datamodel.Goods;
import com.alibaba.taolele.service.SearchSerive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 搜索的controller
 */
@Slf4j
@Controller
public class SearchController {
    @Autowired
    private SearchSerive searchSerive;

    private static final int PAGESIZE = 12;

    /**
     * 搜索
     */
    @RequestMapping(value = "/searchdemo")
    public ModelAndView search(HttpServletRequest request, HttpServletResponse response, Integer currentPage) {
        /*建立modelandView*/
        ModelAndView modelAndView = new ModelAndView("search/searchlist");
        Cookie username = getCookies(request, "username");

        if (username == null) {
            //没有得到cookie直接返回登录页面
            log.info("用户未登录，返回登录界面");
            return new ModelAndView("login/login");
        }

        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        /*获取搜索数据*/
        log.info("搜索关键词为：keyword");
        String keyword = request.getParameter("keyword");
        List<Goods> goodsAndConponsPaging;
        if (currentPage == null) {
            currentPage = 1;
            modelAndView.addObject("currentPage", 1);
        } else {
            modelAndView.addObject("currentPage", currentPage + 1);
        }
        Map<String, Object> res = searchSerive.getGoodsAndConponsPaging("%" + keyword + "%", PAGESIZE, currentPage);
        goodsAndConponsPaging = (List<Goods>) res.get("goods");
        /*放入到modelandview，在对应的jsp页面就可以用goods来引用对应的参数，比如图片之类的*/
        modelAndView.addObject("goods", goodsAndConponsPaging);
        modelAndView.addObject("username", username.getValue());
        modelAndView.addObject("keyword", keyword);
        /*返回最终的视图*/
        return modelAndView;
    }

    /**
     * 获取cookie
     *
     * @param request
     * @param key
     * @return
     */
    private Cookie getCookies(HttpServletRequest request, String key) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(key)) {
                return cookie;
            }
        }
        return null;
    }
    /**
     * 正式搜索
     */
    @RequestMapping(value = "/nullsearch")
    public ModelAndView nullSearch() {
        ModelAndView modelAndView = new ModelAndView("search/searchList");
        ArrayList<Goods> a = (ArrayList<Goods>) searchSerive.nullSearchResult();
       modelAndView.addObject("good", a);
        //modelAndView.getModel().put("good",a);
        return modelAndView;
    }

    @RequestMapping(value = "/keySearch")
    public ModelAndView keySearch(HttpServletRequest request, HttpServletResponse response) {
        String keyWord = request.getParameter("keyword");
        ModelAndView m = new ModelAndView("search/Words_searchList");
        return m;
    }

}
